package com.pine.app.module.security.oauth.user.service;


import com.pine.app.module.security.core.common.AuthConstant;
import com.pine.app.module.security.core.common.SpringContextUtils;
import com.pine.app.module.security.core.userdetails.UserDetails;
import com.pine.app.module.security.core.common.enums.ErrorType;
import com.pine.app.module.security.oauth.exception.AuthenticationException;
import com.pine.app.module.security.oauth.exception.UsernameNotFoundException;
import com.pine.app.module.security.oauth.user.IntegrationLoginRequest;
import com.pine.app.module.security.oauth.user.authenticator.IntegrationAuthenticator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


/**
 * 集成认证用户服务
 * @author xiaoyuan
 * @create 2019/8/2 17:38
 **/

public class IntergrationUserDetailService implements UserDetailService {



    //认证实现类集合
    private List<IntegrationAuthenticator> authenticators;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //如果登录名为空则报错
        if (StringUtils.isBlank(username)) {
            ErrorType.ILLEGAL_USERNAME_OR_PASSWORD.throwThis(AuthenticationException::new);
        }
        //获取当前亲求的request对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = null;
        if (attributes != null) {
            request = attributes.getRequest();
        }
        if (request == null) {
            ErrorType.SERVICE_ERROR.throwThis(AuthenticationException::new);
        }
        //创建集成认证处理类
        IntegrationLoginRequest integrationAuthentication = createAuth(request);
        integrationAuthentication.setUsername(username);
        //获取用户信息
        UserDetails user = this.prepare(integrationAuthentication);
        return user;
    }
    /**
     * 进行预处理
     *
     * @param integrationAuthentication
     */
    private UserDetails prepare(IntegrationLoginRequest integrationAuthentication) {

        if(CollectionUtils.isEmpty(authenticators)){
            authenticators = Collections.emptyList();
            Map<String, IntegrationAuthenticator> beansOfType = SpringContextUtils.getBeanMap(IntegrationAuthenticator.class);
            if (beansOfType != null) {
                this.authenticators =new ArrayList<>( beansOfType.values());
            }
        }
        for(int i=0;i<authenticators.size();i++){
            IntegrationAuthenticator authenticator = authenticators.get(i);
            if (authenticator.support(integrationAuthentication)) {
                return authenticator.authenticate(integrationAuthentication);
            }
        }
        return null;
    }

    private IntegrationLoginRequest createAuth(HttpServletRequest request) {
        //设置集成登录信息
        IntegrationLoginRequest integrationAuthentication = new IntegrationLoginRequest();
        integrationAuthentication.setAuthType(request.getParameter(AuthConstant.auth_type));
        integrationAuthentication.setAuthParameters(request.getParameterMap());
        return integrationAuthentication;
    }

}
